package com.example.jh352160.surfingtv.Index;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;

import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.example.jh352160.surfingtv.Index.customView.Banner;
import com.example.jh352160.surfingtv.Index.customView.IndexColumn;
import com.example.jh352160.surfingtv.Index.customView.IndexThematic;
import com.example.jh352160.surfingtv.Index.customView.Starsight;
import com.example.jh352160.surfingtv.Index.customView.adapter.MainActivityAdapter;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.network.item.IndexItem;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.IndexService;

import java.util.ArrayList;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class MainActivity extends Fragment {

    private static final int NETWORK_OK = 0;
    View view;
    private static IndexItem.DataBean dataBean;
    Button goToTop;
    SwipeToLoadLayout swipeToLoadLayout;

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case NETWORK_OK:
                    init();
            }
        }
    };

    public MainActivity() {}

    public static MainActivity newInstance() {
        return new MainActivity();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //super.onCreate(savedInstanceState);
        view = inflater.inflate(R.layout.activity_main, container, false);
        goToTop = (Button) view.findViewById(R.id.goToTop);
        swipeToLoadLayout = (SwipeToLoadLayout) view.findViewById(R.id.swipetLoad);

        network();

        return view;
    }

    private void network() {
        //api
        Retrofit retrofit = CommonRetrofit.getRetrofit();
        IndexService indexService = retrofit.create(IndexService.class);
        Call<IndexItem> call = indexService.getItem();
        call.enqueue(new Callback<IndexItem>() {
            @Override
            public void onResponse(Call<IndexItem> call, Response<IndexItem> response) {

                dataBean = response.body().getData();
                Message message = new Message();
                message.what = NETWORK_OK;
                handler.sendMessage(message);
            }

            @Override
            public void onFailure(Call<IndexItem> call, Throwable t) {
                Log.e("Retrofit Error", t.toString());
            }
        });
    }

    private void init() {
        final ListView listView = (ListView) view.findViewById(R.id.swipe_target);
        List<View> viewList = new ArrayList<>();
        //listView.addHeaderView(new Banner(this.getActivity()));
        viewList.add(new Banner(this.getActivity()));
        viewList.add(new IndexThematic(this.getActivity()));
        viewList.add(new IndexColumn(this.getActivity()));
        viewList.add(new Starsight(this.getActivity()));
        MainActivityAdapter adapter = new MainActivityAdapter(this.getActivity(), android.R.layout.simple_expandable_list_item_1, viewList);
        listView.setAdapter(adapter);
    }

//        listView.setOnScrollListener(new AbsListView.OnScrollListener() {
//            @Override
//            public void onScrollStateChanged(AbsListView absListView, int i) {
//
//            }
//
//            @Override
//            public void onScroll(AbsListView absListView, int i, int i1, int i2) {
//                if (i != 0) {
//                    goToTop.setVisibility(View.VISIBLE);
//                } else {
//                    goToTop.setVisibility(View.GONE);
//                }
//            }
//        });

//        goToTop.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View view) {
//                listView.setSelection(0);
//            }
//        });
//
//        swipeToLoadLayout.setOnRefreshListener(new OnRefreshListener() {
//            @Override
//            public void onRefresh() {
//                new AsyncTask<Void, Void, Void>() {
//                    @Override
//                    protected Void doInBackground(Void... voids) {
//                        //network();
//                        return null;
//                    }
//
//                    @Override
//                    protected void onPostExecute(Void aVoid) {
//                        super.onPostExecute(aVoid);
//                        swipeToLoadLayout.setRefreshing(false);
//                    }
//                }.execute();
//            }
//        });
//
//    }

    public static IndexItem.DataBean getData() {
        return dataBean;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
